package com.wintop.youyuan.ajax;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.wintop.youyuan.model.BaseData;
import com.wintop.youyuan.model.DetailData;
import com.wintop.youyuan.model.Luck;
import com.wintop.youyuan.model.Message;
import com.wintop.youyuan.model.PersonalsCondition;
import com.wintop.youyuan.model.Photo;
import com.wintop.youyuan.model.User;
import com.wintop.youyuan.services.MailBoxServices;
import com.wintop.youyuan.util.ActionUtil;

@Scope("prototype")
@Controller
public class MailBoxAjax extends ActionSupport implements RequestAware,
		SessionAware, ApplicationAware, ServletResponseAware {
	private static final long serialVersionUID = 1L;
	private Map<String, Object> request;
	private Map<String, Object> session;
	private Map<String, Object> application;
	private HttpServletResponse response;
	private User user;
	private MailBoxServices mailBoxServices;
	private BaseData baseData;
	private DetailData detailData;
	private PersonalsCondition personalsCondition;
	private Message message;
	private Photo photo;
	private HashMap<String, Object> otherData;// 临时数据储存
	private Luck luck;

	/*
	 * 添加消息
	 */
	public void addMessage() {
		user = (User) session.get("userInfo");
		message.setSendUser(user.getUserid());
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd h:mm:ss");
		try {
			Date times = df.parse((String) otherData.get("time"));
			message.setTime(times);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		boolean isAdd = mailBoxServices.addMailInformation(message);
		printString(isAdd);
	}

	/*
	 * 根据id删除消息
	 */
	public void deleteMessageById() {
		boolean isAdd = mailBoxServices.deleteMailInformation(message);
		printString(isAdd);
	}

	/*
	 * 根据用户id删除消息
	 */
	public void deleteMessageByUserId() {
		user = (User) session.get("userInfo");
		boolean isAdd = mailBoxServices.deleteMessages(user.getUserid(),
				message.getSendUser());
		printString(isAdd);
	}

	/*
	 *  删除最近联系
	 */
	public void deleteRecentMessage() {
		user = (User) session.get("userInfo");
		boolean isAdd = mailBoxServices.deleteMessages(message.getSendUser(),
				user.getUserid());
		printString(isAdd);
	}

	/*
	 * 打招呼
	 */
	public void sayHello() {
		user = (User) session.get("userInfo");
		message.setSendUser(user.getUserid());
		message.setContent(ActionUtil.getSayHello());
		message.setTime(new Date(System.currentTimeMillis()));
		boolean isAdd = mailBoxServices.addMailInformation(message);
		printString(isAdd);
	}

	/*
	 * 索要照片
	 */
	public void needPhoto() {
		user = (User) session.get("userInfo");
		message.setSendUser(user.getUserid());
		message.setContent("你好啊，能不能发张照片看看呢？");
		message.setTime(new Date(System.currentTimeMillis()));
		boolean isAdd = mailBoxServices.addMailInformation(message);
		printString(isAdd);
	}

	/*
	 * 根据Id取消关注
	 */
	public void deleteLuckById() {
		boolean isAdd = mailBoxServices.deleteMailInformation(luck);
		printString(isAdd);
	}

	/*
	 * 关注
	 */
	public void follow() {
		user = (User) session.get("userInfo");
		luck.setUserid(user.getUserid());
		luck.setProperty("myAttention");
		luck.setTime(new Date(System.currentTimeMillis()));
		boolean isAdd = mailBoxServices.addMailInformation(luck);
		printString(isAdd);
	}

	private void printString(Object str) {
		PrintWriter out = null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		out.print(str);
		out.close();
	}

	public Luck getLuck() {
		return luck;
	}

	public void setLuck(Luck luck) {
		this.luck = luck;
	}

	public Map<String, Object> getRequest() {
		return request;
	}

	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public Map<String, Object> getApplication() {
		return application;
	}

	public void setApplication(Map<String, Object> application) {
		this.application = application;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public BaseData getBaseData() {
		return baseData;
	}

	public void setBaseData(BaseData baseData) {
		this.baseData = baseData;
	}

	public DetailData getDetailData() {
		return detailData;
	}

	public void setDetailData(DetailData detailData) {
		this.detailData = detailData;
	}

	public PersonalsCondition getPersonalsCondition() {
		return personalsCondition;
	}

	public void setPersonalsCondition(PersonalsCondition personalsCondition) {
		this.personalsCondition = personalsCondition;
	}

	public Message getMessage() {
		return message;
	}

	public void setMessage(Message message) {
		this.message = message;
	}

	public Photo getPhoto() {
		return photo;
	}

	public void setPhoto(Photo photo) {
		this.photo = photo;
	}

	public HashMap<String, Object> getOtherData() {
		return otherData;
	}

	public void setOtherData(HashMap<String, Object> otherData) {
		this.otherData = otherData;
	}

	@Resource(name = "mailBoxServices")
	public void setMailBoxServices(MailBoxServices mailBoxServices) {
		this.mailBoxServices = mailBoxServices;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

}
